package com.pan.service.impl;

import com.pan.annotation.GoodsExist;
import com.pan.common.RedisKeyName;
import com.pan.entity.Result;
import com.pan.enume.ResultEnum;
import com.pan.excep.error.ServiceException;
import com.pan.service.CartService;
import com.pan.tools.RedisTools;
import com.pan.vo.Cart;
import com.pan.vo.CartVo;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service("cartService")
public class CartServiceImpl implements CartService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private RedisTools redisTools;


    @GoodsExist
    public void add(Long userId,Long goodsId,Integer number){
        String key = RedisKeyName.CART_NAME+ userId;
        stringRedisTemplate.opsForHash()
                .put(key,String.valueOf(goodsId),String.valueOf(number));
    }


    @Override
    public Result addCart(String userTicket,List<CartVo> cartVoList){
        Long userId = Long.valueOf( (String)redisTools.hget("user:"+userTicket,"id").getData());
        cartVoList.stream().forEach(cartVo -> {
            Integer number = cartVo.getNumber();
            if (number<=0||number>99){
                throw new ServiceException(403,"商品："+cartVo.getGoodsId()+"，数量错误");
            }
            add(userId,cartVo.getGoodsId(),number);
        });

        return new Result(ResultEnum.SUCCESS);

    }

    @Override
    public Result<List<Cart>> getAllCartByUser(String userTicket){
        Long userId = Long.valueOf((String) redisTools.hget(RedisKeyName.USER_NAME+userTicket,"id").getData());
        Map<Object, Object> map = redisTools.hgetall(RedisKeyName.CART_NAME+String.valueOf(userId)).getData();
        Iterator<Map.Entry<Object,Object>> iterator = map.entrySet().iterator();

        List<Cart> li_data = new ArrayList<>();
        while (iterator.hasNext()){
            Map.Entry entry = iterator.next();
            Long goodsId = Long.parseLong((String) entry.getKey());
            Integer number = Integer.parseInt((String) entry.getValue());
            Cart cart = new Cart(userId,goodsId,number);
            li_data.add(cart);
        }
        Result<List<Cart>> result = new Result(ResultEnum.SUCCESS,li_data);
        return result;
    }

    @Override
    public Result deleteGoods(String userTicket, Long goodsId){
        String userId = (String)stringRedisTemplate.opsForHash().get(RedisKeyName.USER_NAME+userTicket,"id");
        stringRedisTemplate.opsForHash().delete(RedisKeyName.CART_NAME+userId,String.valueOf(goodsId));
        return new Result(ResultEnum.SUCCESS);
    }

    @Override
    public Result updateGoodsCount(String userTicket, Long goodsId, Integer count){
        if (count <= -99 || count>99){
            return new Result(403,"修改数量错误");
        }
        Long userId = Long.parseLong((String)  stringRedisTemplate.opsForHash().get(RedisKeyName.USER_NAME+userTicket,"id"));
        String key = RedisKeyName.CART_NAME +userId;
        boolean isExist = redisTools.stringRedisTemplate.opsForHash().hasKey(key,String.valueOf(goodsId));
        if (!isExist){
            return new Result(ResultEnum.NOT_FOUND_ERROR);
        }
        stringRedisTemplate.opsForHash().increment(RedisKeyName.CART_NAME+userId,String.valueOf(goodsId),count);
        Result result = new Result(ResultEnum.SUCCESS);
        return result;
    }

}
